/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.ui.controls
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class ControlLabel extends Sprite
	{
		public var margin:Number;
		
		protected var backGround:Sprite;	
		protected var labelBackGround:Sprite;
		protected var autoSize:String;
		protected var backgroundSize:Point;
		
		public function ControlLabel(LabelBackGround:Class, label:String = null, margin:Number = 3, textFildAutoSize:String = TextFieldAutoSize.LEFT)
		{
			this.margin = margin;
			this.autoSize = textFildAutoSize;			
			
			backGround = new Sprite();
			addChild(backGround);
			
			labelBackGround = new LabelBackGround();			
			addChild(labelBackGround);
			
			if(label){
				this.label = label;
			}else{
				resize();
			}
		}
		
		public function set size(p:Point):void{
			if(p){
				autoSize = TextFieldAutoSize.NONE;
				
				if(!p.equals(backgroundSize)){					
					backgroundSize = p;					
				}
				
				resize();
			}
		}
		public function get size():Point{
			return backgroundSize;
		}
		
		public function resize():void{
			labelBackGround.x = margin;
			if(labelBackGround.hasOwnProperty("label")){
				var textFild:TextField = (labelBackGround as Object).label;
				
				if(autoSize != TextFieldAutoSize.NONE){
					backgroundSize = new Point(labelBackGround.width, labelBackGround.height);
				}else{
					textFild.width = labelBackGround.width;
					textFild.height = labelBackGround.height;
				}
				
				textFild.autoSize = autoSize;
			}
			
			backGround.graphics.clear();
			if(labelBackGround){
				backGround.graphics.beginFill(0x000000,0);
				backGround.graphics.drawRect(0,0, backgroundSize.x + (margin*2), backgroundSize.y);
				backGround.graphics.endFill();
			}
		}
		
		public function set label(value:String):void{
			if(!value){
				value = new String(); 
			}
			if(labelBackGround.hasOwnProperty("label")){
				var textFild:TextField = (labelBackGround as Object).label;
				textFild.text = value;
			}			
			resize();
		}
		
		public function get label():String{
			if(labelBackGround.hasOwnProperty("label")){
				var textFild:TextField = (labelBackGround as Object).label;
				return textFild.text;
			}
			return new String();
		}
	}
}